package org.tinaelle.client.gui.fieldSet;

import org.tinaelle.client.gui.grid.DataDetailItf;
import org.tinaelle.client.gui.list.ListProvider;
import org.tinaelle.client.utils.DefaultAsyncCallback;
import org.tinaelle.client.utils.Utilities;
import org.tinaelle.common.dto.AdUserDTO;
import org.tinaelle.common.dto.AdUserDTORecord;
import org.tinaelle.server.service.AdSecurityServiceAsync;

import com.gwtext.client.data.Record;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.form.Checkbox;
import com.gwtext.client.widgets.form.ComboBox;
import com.gwtext.client.widgets.form.DateField;
import com.gwtext.client.widgets.form.Field;
import com.gwtext.client.widgets.form.MultiFieldPanel;
import com.gwtext.client.widgets.form.NumberField;
import com.gwtext.client.widgets.form.TimeField;
import com.gwtext.client.widgets.layout.AnchorLayoutData;
import com.gwtext.client.widgets.layout.ColumnLayout;
import com.gwtext.client.widgets.layout.ColumnLayoutData;
import com.gwtext.client.widgets.layout.FormLayout;

public abstract class AbstractDataDetail implements DataDetailItf {
	protected Field[]	defaultFields	= {};

	protected Field[][]	fields;

	protected ComboBox	createdByComboBox;
	protected ComboBox	updatedByComboBox;

	public void initFields() {
		int idx = 0;
		defaultFields = new Field[ 8 ];
		defaultFields[ idx++ ] = new Checkbox( "Active", "isActive" );

		final DateField created = new DateField( "Created", "created", 110 );
		defaultFields[ idx++ ] = created;
		created.setDisabled( true );

		final TimeField createdT = new TimeField( "", "createdT", 80 );
		defaultFields[ idx++ ] = createdT;
		createdT.setDisabled( true );
		createdT.setHideLabel( true );
		createdT.setIncrement( 1 );

		createdByComboBox = ListProvider.getInstance().getAdUserCombox( "Created by", "createdBy" );
		defaultFields[ idx++ ] = createdByComboBox;
		createdByComboBox.setDisabled( true );

		final DateField updated = new DateField( "Updated", "updated", 110 );
		defaultFields[ idx++ ] = updated;
		updated.setDisabled( true );

		final TimeField updatedT = new TimeField( "", "updatedT", 80 );
		defaultFields[ idx++ ] = updatedT;
		updatedT.setDisabled( true );
		updatedT.setHideLabel( true );
		updatedT.setIncrement( 1 );

		updatedByComboBox = ListProvider.getInstance().getAdUserCombox( "Updated by", "updatedBy" );
		defaultFields[ idx++ ] = updatedByComboBox;
		updatedByComboBox.setDisabled( true );

		defaultFields[ idx++ ] = new NumberField( "Version", "version" );
		defaultFields[ idx - 1 ].setDisabled( true );
	}

	public void preloadCombox(final Record record) {
		preloadCreatedBy( record.getAsString( "createdBy" ) );
		preloadUpdatedBy( record.getAsString( "updatedBy" ) );
	}

	public void preloadCreatedBy(final String createdBy) {
		final Store store = createdByComboBox.getStore();
		// Log.debug( "preloadCreatedBy:: query ( '" + createdBy + "' ) = " +
		// store.query( "adUserId", createdBy ) );
		if ( ( store.getById( createdBy ) != null ) && !Utilities.isBlankOrNull( createdBy ) ) {
			AdSecurityServiceAsync.Util.getInstance().getUserById( Integer.valueOf( createdBy ),
			        new DefaultAsyncCallback<AdUserDTO>() {
				        public void onSuccess(final AdUserDTO result) {
					        if ( result != null ) {
						        store.add( AdUserDTORecord.getInstance().dtoToRecord( result ) );
					        }
				        }
			        } );
		}
	}

	public void preloadUpdatedBy(final String updatedBy) {
		final Store store = updatedByComboBox.getStore();
		if ( ( store.getById( updatedBy ) != null ) && !Utilities.isBlankOrNull( updatedBy ) ) {
			AdSecurityServiceAsync.Util.getInstance().getUserById( Integer.valueOf( updatedBy ),
			        new DefaultAsyncCallback<AdUserDTO>() {
				        public void onSuccess(final AdUserDTO result) {
					        if ( result != null ) {
						        store.add( AdUserDTORecord.getInstance().dtoToRecord( result ) );
					        }
				        }
			        } );
		}
	}

	public void addFields(final Field[][] fields) {
		initFields();
		this.fields = new Field[ fields.length + 1 ][];
		for ( int i = 0; i < fields.length; i++ ) {
			this.fields[ i ] = fields[ i ];
		}
		this.fields[ fields.length ] = defaultFields;
	}

	public Panel getFieldPanel() {
		final Panel panel = new Panel();
		panel.setLayout( new ColumnLayout() );
		Panel columnPanel;
		for ( int i = 0; i < fields.length; i++ ) {
			columnPanel = new Panel();
			columnPanel.setLayout( new FormLayout() );
			for ( int j = 0; j < fields[ i ].length; j++ ) {
				if ( ( fields[ i ][ j ] instanceof DateField ) && ( ( j + 1 ) < fields[ i ].length )
				        && ( fields[ i ][ j + 1 ] instanceof TimeField ) && fields[ i ][ j + 1 ].isHideLabel() ) {
					final MultiFieldPanel dateTimePanel = new MultiFieldPanel();
					dateTimePanel.addToRow( fields[ i ][ j ], 220 );
					fields[ i ][ j + 1 ].setWidth( 80 );
					dateTimePanel.addToRow( fields[ i ][ j + 1 ], new ColumnLayoutData( 1 ) );
					columnPanel.add( dateTimePanel, new AnchorLayoutData( "95%" ) );
					j++;
					continue;
				}
				if ( fields[ i ][ j ] != null ) {
					columnPanel.add( fields[ i ][ j ], new AnchorLayoutData( "95%" ) );
				}
			}
			panel.add( columnPanel, new ColumnLayoutData( 1.0 / fields.length ) );
		}
		return panel;
	}
}
